package com.github.jin.simpledropdownmenu

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import org.jetbrains.anko.find
import org.jetbrains.anko.imageResource
import org.jetbrains.anko.onClick

/**
 * @author Jin .
 * creation date 2017/11/6 10:00.
 * modification date 2017/11/6 10:00.
 * describe
 */
class SimpleDropDownMenuAdapter(private val itemList: MutableList<MenuItem>) : RecyclerView.Adapter<SimpleDropDownMenuAdapter.VH>() {
    var itemClickListener: ItemClickListener? = null

    interface ItemClickListener {
        fun onItemClick(position: Int)
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): VH {
        val view = LayoutInflater.from(parent?.context).inflate(R.layout.item_menu, parent, false)
        return VH(view)
    }

    override fun getItemCount(): Int = itemList.size

    override fun onBindViewHolder(holder: VH?, position: Int) {
        holder?.itemTitleTextView?.text = itemList[position].itemTitle
        holder?.itemIconImgView?.imageResource = itemList[position].itemIcon
        holder?.itemLinear?.onClick {
            itemClickListener?.onItemClick(position)
        }
    }

    class VH internal constructor(view: View) : RecyclerView.ViewHolder(view) {
        var itemLinear = view.find<LinearLayout>(R.id.item_linear)
        var itemIconImgView = view.find<ImageView>(R.id.item_img_icon)
        var itemTitleTextView = view.find<TextView>(R.id.item_txt_title)
    }
}